/**
 * $Id: LoginUrlEntryPoint.java 19 2011-06-11 04:06:06Z feigme $
 */
package com.iary.interfaces.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

public class LoginUrlEntryPoint implements AuthenticationEntryPoint {

	private String adminLoginUrl;
	private String onlineLoginUrl;

	public void commence(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException authentication)
			throws IOException, ServletException {
		String targetUrl;
		String uri = request.getRequestURI();
		if (StringUtils.indexOf(uri, "/admin") > -1) {
			targetUrl = adminLoginUrl;
		} else {
			targetUrl = onlineLoginUrl;
		}

		response.sendRedirect(request.getContextPath() + targetUrl);

	}

	public String getAdminLoginUrl() {
		return adminLoginUrl;
	}

	public void setAdminLoginUrl(String adminLoginUrl) {
		this.adminLoginUrl = adminLoginUrl;
	}

	public String getOnlineLoginUrl() {
		return onlineLoginUrl;
	}

	public void setOnlineLoginUrl(String onlineLoginUrl) {
		this.onlineLoginUrl = onlineLoginUrl;
	}

}
